package Rooms;

import Entities.OrderCalendarEntity;
import Entities.RoomSearchEntity;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.List;
import Beans.EJB_Filters.IRoomListFlt;
import com.UtilsPak.DateTimeFunctions;
import org.ajax4jsf.component.UIRepeat;


/**
 * Created by IntelliJ IDEA.
 * User: mchebanenko
 * Date: 25-Feb-2011
 * Time: 11:17:55 AM
 * To change this template use File | Settings | File Templates.
 */
public class RoomDetails extends DateTimeFunctions {
    private RoomSearchEntity room = new RoomSearchEntity();
    private List<OrderCalendarEntity> orderCalendar;
    private UIRepeat repeatComp;
    private int colspan;

    public RoomListFilter getFlt() {
        return (RoomListFilter) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("RoomListFilter");
    }

    public RoomSearchEntity getRoom() {
        return room;
    }

    public void setRoom(RoomSearchEntity room) {
        this.room = room;
    }

    public List<OrderCalendarEntity> getOrderCalendar (){
        if (orderCalendar == null) {
            try{
                RoomListFilter flt = getFlt();
                Context cont = new InitialContext();
                orderCalendar = ((IRoomListFlt) cont.lookup("HotelManager/RoomListFltBean/local")).getOrderCalendarList(flt.getFlt().getDateFrom(), flt.getFlt().getDateTo(),room.getRoom_id().toString());
            } catch (Exception e) {
                e.printStackTrace();
                return  null;
            }
        }
        return orderCalendar;
    }


    public String InitRoomDetails(RoomSearchEntity room) {
        String retVal = "";
        if (room != null){
            setRoom(room);
            retVal="ShowRoomDetails";
        }
        return  retVal;
    }

    public UIRepeat getRepeatComp() {
        return repeatComp;
    }

    public void setRepeatComp(UIRepeat repeatComp) {
        this.repeatComp = repeatComp;
    }

    public Boolean displayMonth(String data) {

        System.out.println(data);   //todo worng day in moth count logic


        if (repeatComp.getRowIndex() == 0) {
            colspan = 0;
        }

        if (getDayOfMonth(data).equals("1")) {
            colspan = 1;
        } else {
            colspan++;
        }

        if (getDaysInMonth(data).equals(getDayOfMonth(data))) {
            colspan++;
            return true;
        }

        if ((repeatComp.getRowCount() - 1) == repeatComp.getRowIndex()) {
            return true;
        } else {
            return false;
        }
    }

    public int getColspan() {
        return colspan;
    }

}
